package online.yysh.charging.base;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import online.yysh.charging.bean.Login;
import online.yysh.charging.livedata.UserManager;
import online.yysh.charging.utils.Constant;

/**
 * Created by acer on 2017/6/16.
 */
public abstract class BaseFragment extends Fragment {
    public String TAG = getClass().getSimpleName();

    protected String openid = "";
//    protected String oid = "";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = initView(inflater, container, savedInstanceState);
        return view;
    }

    public abstract View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    public abstract void initData(Bundle savedInstanceState);

    public void showToast(final String msg){
        if ("main".equals(Thread.currentThread().getName())){
            Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
        }else{
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SharedPreferences sp = getActivity().getSharedPreferences(Constant.SP_NAME, Context.MODE_PRIVATE);
        openid = sp.getString(Constant.SP_KEY_OPENID,"");
//        oid = sp.getString(Constant.SP_KEY_OID,"");
        UserManager.getInstance().getLiveData().observe(getActivity(), new Observer<Login>() {
            @Override
            public void onChanged(Login login) {
                openid = login.getOpenid();
            }
        });
        initData(savedInstanceState);
    }
}
